package com.example.demo.service;

import com.example.demo.entity.IEntity;
import org.jooq.impl.UpdatableRecordImpl;

public class BaseService<E extends IEntity> extends QueryService<E> {

    public int insert(E entity) {
        UpdatableRecordImpl<?> record = (UpdatableRecordImpl<?>) this.getCtx().newRecord(this.getTable(), entity);
        int n = record.insert();
        entity.setId(record.get("id", Long.class));
        return n;
    }

    public int update(E entity) {
        UpdatableRecordImpl<?> record = (UpdatableRecordImpl<?>) this.getCtx().newRecord(this.getTable(), entity);
        return record.update();
    }

    public int delete(E entity) {
        UpdatableRecordImpl<?> record = (UpdatableRecordImpl<?>) this.getCtx().newRecord(this.getTable(), entity);
        return record.delete();
    }
}
